﻿using ALMDDD.AspNetCore;
using ALMDDD.Domain.Interfaces;
using ALMDDD.Infrastructure.Context;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ALMDDD.Infrastructure.Repository
{
    /// <summary>
    /// 仓储基类
    /// </summary>
    public class Repository<Tentity> : IRepository<Tentity> where Tentity : class
    {

        protected readonly DbContext _db;//上下文对象
        //protected readonly DbSet<Tentity> _dbSet;//实体集合对象
        public List<Expression<Func<Tentity, object>>> IncludeEntity { get; set; }

        public Repository(DbContext context)
        {
          
            _db = context;
          
            IncludeEntity = new List<Expression<Func<Tentity, object>>>();
        }

        public DbSet<Tentity> Dbset
        {
            get
            {
                return _db.Set<Tentity>();
            }
        }
        public IQueryable<Tentity> Query
        {
            get
            {
                var dbSet = Dbset.AsQueryable();
                foreach (var entity in IncludeEntity)
                {
                    dbSet = dbSet.Include(entity);
                }
                return dbSet;
            }

        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="tentity"></param>
        public void Add(Tentity tentity)
        {
            Dbset.Add(tentity);
        }
        /// <summary>
        /// 手动回收
        /// </summary>
        public void Dispose()
        {
            _db.Dispose();
        }
        /// <summary>
        /// 获取所有实体信息
        /// </summary>
        /// <returns></returns>
        public IQueryable<Tentity> GetAll()
        {
            return Dbset;
        }
        /// <summary>
        /// 根据id来获取实体信息
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns></returns>
        public Tentity GetById(Guid id)
        {
            return Dbset.Find(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public Tentity GetMany(Expression<Func<Tentity, bool>> where)
        {
            Tentity s = Dbset.Where(where).FirstOrDefault();
            return s;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="paging">分页参数</param>
        /// <returns></returns>
        public Tuple<int, int, IEnumerable<Tentity>> GetMany(Expression<Func<Tentity, bool>> where, Paging paging)
        {
            var dbSet = Dbset;
            if (paging == null)
            {
                paging = new Paging { PageIndex = 1, PageSize = int.MaxValue };

            }
            if (where != null)
                dbSet.Where(where);

            paging.RecordCount = dbSet.Count();
            var result = dbSet.Skip(paging.SkipCount).Take(paging.PageSize).AsNoTracking().ToList();
            return new Tuple<int, int, IEnumerable<Tentity>>(paging.PageCount,paging.RecordCount,result);

           
        }

        /// <summary>
        /// 根据id删除数据
        /// </summary>
        /// <param name="id"></param>
        public void Remove(Guid id)
        {
            Dbset.Remove(Dbset.Find(id));
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return _db.SaveChanges();
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="obj"></param>
        public void Update(Tentity obj)
        {
            Dbset.Update(obj);
        }

    }
}
